import {ipcRenderer} from "electron";
import ResultDTO from "../model/ResultDTO";
import MySqlConnectInfo from "../model/mysql/MySqlConnectInfo";

const sqliteGetConnectInfoList = (): Promise<ResultDTO<MySqlConnectInfo[]>> => {
    return new Promise<ResultDTO<MySqlConnectInfo[]>>((resolve, reject) => {

        ipcRenderer.invoke('sqlite-get-connect-info-list')
            .then((res: string) => {
                    let rrs: ResultDTO<MySqlConnectInfo[]> = JSON.parse(res)
                    console.log("rrs", rrs)
                    resolve(rrs)
                }
            ).catch((res: string) => {
                reject(JSON.parse(res))
            }
        );
    })

}
const sqliteUpdateConnectInfo = (connectInfo: MySqlConnectInfo): Promise<ResultDTO<any>> => {
    return new Promise<ResultDTO<MySqlConnectInfo[]>>((resolve, reject) => {

        //更新
        ipcRenderer.invoke('sqlite-update-connection-info', JSON.stringify(connectInfo))
            .then((res: string) => {
                    resolve(JSON.parse(res))
                }
            ).catch((res: string) => {
                reject(JSON.parse(res))
            }
        );
    })
}
const sqliteAddConnectInfo = (connectInfo: MySqlConnectInfo): Promise<ResultDTO<any>> => {
    return new Promise<ResultDTO<MySqlConnectInfo[]>>((resolve, reject) => {
        //更新
        ipcRenderer.invoke('sqlite-add-connection-info', JSON.stringify(connectInfo))
            .then((res: string) => {
                    resolve(JSON.parse(res))
                }
            ).catch((res: string) => {
                reject(JSON.parse(res))
            }
        );
    })
}
const sqliteDeleteConnectInfo = (connectInfo: MySqlConnectInfo): Promise<ResultDTO<any>> => {
    if (!connectInfo){
        return
    }
    return new Promise<ResultDTO<MySqlConnectInfo[]>>((resolve, reject) => {
        //更新
        ipcRenderer.invoke('sqlite-delete-connection-info', JSON.stringify(connectInfo))
            .then((res: string) => {
                    resolve(JSON.parse(res))
                }
            ).catch((res: string) => {
                reject(JSON.parse(res))
            }
        );
    })
}
export {
    sqliteGetConnectInfoList, sqliteUpdateConnectInfo, sqliteAddConnectInfo,sqliteDeleteConnectInfo
}